package year2021.month0567;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTest {

    public static void main(String[] args) {
        timeZone();
//        Date thisWeekMonday = getThisWeekMonday(new Date());
//        System.out.println(thisWeekMonday);
    }

    public static void timeZone() {
        //显示Java能识别的系统中所有正确的时区
        String[] ids = TimeZone.getAvailableIDs();
        for (int i = 0; i < ids.length; i++){
            System.out.println(ids[i]);
        }

        System.out.println("原时间 " + new Date());

        TimeZone time = TimeZone.getTimeZone("ETC/GMT+12");

        TimeZone.setDefault(time);

        System.out.println("修改后时间 " + new Date());
    }

    public static Date getThisWeekMonday(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }
}
